﻿package mortal.game.view.mainUI.avatar.view
{
    import __AS3__.vec.*;
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.mui.controls.*;
    import flash.display.*;
    import flash.filters.*;
    import flash.geom.*;
    import flash.utils.*;
    import mortal.component.gconst.*;
    import mortal.game.manager.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;

    public class CombatEffect extends GSprite
    {
        private var _comBat:BitmapNumberText;
        private var _comBatChangeTxt:BitmapNumberText;
        private var _bg:GBitmap;
        private var _txtBitmap:GBitmap;
        private var _sp:Sprite;
        private var _oldValue:String;
        private var _newValue:String;
        private var _showTween:TweenMax;
        private var _timeId:int;
        private var _stepVec:Vector.<Function>;
        private var _currentFunIndex:int = 0;
        private static var _instance:CombatEffect;

        public function CombatEffect()
        {
            this._stepVec = new Vector.<Function>;
            return;
        }// end function

        override protected function configUI() : void
        {
            super.configUI();
            this._sp = new Sprite();
            this._sp.y = 30;
            this._bg = UIFactory.gBitmap(ImagesConst.RollCombatBg, -265, -38, this._sp);
            this._txtBitmap = UIFactory.gBitmap(ImagesConst.RollCombat, 0, 0, this);
            this._comBat = UIFactory.bitmapNumberText(150, 10, "RollCombatNum.png", 41, 39, -6, this, 9);
            this._comBatChangeTxt = UIFactory.bitmapNumberText(150, 0, "RollCombatChangeNum.png", 41, 39, -6, null);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            var _loc_1:Boolean = false;
            this.mouseChildren = false;
            this.mouseEnabled = _loc_1;
            return;
        }// end function

        public function showEffect(param1:String, param2:String) : void
        {
            var _loc_3:* = param1;
            this._comBat.text = param1;
            this._oldValue = _loc_3;
            this._newValue = param2;
            this.initialization();
            this._stepVec.push(this.showCombat, this.showBg, this.showFilter, this.rollingValue, this.showChange, this.hideChange, this.showAlpha, this.hideEffect);
            this.runFunction();
            return;
        }// end function

        private function runFunction(... args) : void
        {
            this._stepVec[this._currentFunIndex].call(null, args);
            args = this;
            var _loc_3:* = args._currentFunIndex + 1;
            args._currentFunIndex = _loc_3;
            return;
        }// end function

        private function setDurationTime(param1:Number) : void
        {
            this._timeId = setTimeout(this.runFunction, param1 * 1000);
            return;
        }// end function

        private function initialization() : void
        {
            this._currentFunIndex = 0;
            clearTimeout(this._timeId);
            if (this._showTween && this._showTween != null)
            {
                this._showTween.kill();
            }
            if (this._sp.parent)
            {
                this.removeChild(this._sp);
            }
            if (this._comBatChangeTxt.parent)
            {
                this.removeChild(this._comBatChangeTxt);
            }
            LayerManager.highestLayer.addChild(this);
            this.transform.matrix = new Matrix(1, 0, 0, 1, 300, 300);
            this.scaleX = 2.5;
            this.scaleY = 2.5;
            this.x = SceneRange.display.width - 531 >> 1;
            this.y = SceneRange.display.height - this.height >> 1;
            this.alpha = 1;
            this.filters = [];
            return;
        }// end function

        private function showCombat(param1:String) : void
        {
            this._showTween = TweenMax.to(this, 0.3, {x:(this._comBat.x + param1.length * 33) * 2.5 * (1 - 1 / 2.5) / 2 + this.x, y:this.height * (1 - 1 / 2.5) / 2 + this.y, alpha:1, scaleX:1, scaleY:1, ease:Back.easeOut});
            this.setDurationTime(0.1);
            return;
        }// end function

        private function showBg(param1:Object) : void
        {
            addChildAt(this._sp, 0);
            this._sp.alpha = 0;
            this._sp.x = this._comBat.x + this._comBat.width >> 1;
            this._sp.scaleX = 2.5;
            this._sp.scaleY = 2.5;
            this._showTween = TweenMax.to(this._sp, 0.3, {alpha:1, scaleX:1, scaleY:1, ease:Quint.easeOut, ease:Back.easeOut});
            this.setDurationTime(0.5);
            return;
        }// end function

        private function showFilter(param1:Object) : void
        {
            EffectManager.glowFilterReg(this, [FilterConst.colorGlowFilter(11252925)], 1, 13, 1, 1);
            this.setDurationTime(0.3);
            return;
        }// end function

        private function rollingValue(param1:Object) : void
        {
            EffectManager.glowFilterUnReg(this);
            this._comBat.addToValue(int(this._newValue));
            this.setDurationTime(1);
            return;
        }// end function

        private function showChange(param1:Object) : void
        {
            this.addChild(this._comBatChangeTxt);
            this._comBatChangeTxt.y = -20;
            this._comBatChangeTxt.alpha = 0;
            this._comBatChangeTxt.text = "+" + (int(this._comBat.text) - int(this._oldValue));
            this._showTween = TweenMax.to(this._comBatChangeTxt, 0.3, {alpha:1, y:this._comBatChangeTxt.y - 35, ease:Quint.easeOut});
            this.setDurationTime(0.5);
            return;
        }// end function

        private function hideChange(param1:Object) : void
        {
            this._showTween = TweenMax.to(this._comBatChangeTxt, 0.2, {alpha:0, y:this._comBatChangeTxt.y - 50, ease:Quint.easeOut});
            this.setDurationTime(0.2);
            return;
        }// end function

        private function showAlpha(param1:Object) : void
        {
            this.transform.matrix = new Matrix(1, 0, -0.5, 1, this.x, this.y);
            this._showTween = TweenMax.to(this, 0.1, {alpha:0, x:this.x - 200});
            this.filters = [new BlurFilter(20, 0)];
            this.setDurationTime(0.1);
            return;
        }// end function

        private function hideEffect(param1:Object) : void
        {
            LayerManager.highestLayer.removeChild(this);
            this.removeChild(this._comBatChangeTxt);
            if (this._showTween != null)
            {
                this._showTween.kill();
            }
            clearTimeout(this._timeId);
            return;
        }// end function

        public static function get instance() : CombatEffect
        {
            if (_instance == null)
            {
                _instance = new CombatEffect;
            }
            return _instance;
        }// end function

    }
}
